<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('日程类型')" prop="entourage">
            <dictSelect
              v-model="form.dailyType"
              :clearable="false"
              dictionary="rclx"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('日程人员')" prop="scheduleStaffId">
            <UserSelect
              v-model="form.scheduleStaffId"
              :placeholder="$t('日程人员')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('部门')" prop="department">
            <DeptSelect v-model="form.department" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开始时间')" prop="startTime">
            <el-date-picker
              v-model="form.startTime"
              clearable
              style="width: 200px"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              default-time="9:00:00"
              :placeholder="$t('开始时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('结束时间')" prop="finishTime">
            <el-date-picker
              v-model="form.finishTime"
              clearable
              style="width: 200px"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              default-time="18:00:00"
              :placeholder="$t('结束时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('随行人员')" prop="entourage">
            <UserSelect
              v-model="form.entourage"
              :multiple="true"
              :placeholder="$t('随行人员')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公开范围')" prop="entourage">
            <span slot="label">
              {{ $t('公开范围') }}
              <el-tooltip placement="top">
                <div slot="content">私密仅本人及admin可见 </div>
                <i class="el-icon-question" />
              </el-tooltip>
            </span>
            <dictSelect
              v-model="form.publicScope"
              :clearable="false"
              dictionary="gkfw"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否出差')" prop="travel">
            <el-radio-group v-model="form.travel">
              <el-radio
                v-for="dict in travelOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('提醒')" prop="entourage">
            <dictSelect
              v-model="form.remind"
              :clearable="true"
              dictionary="remind_type"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('完成状态')" prop="invoiceType">
            <el-select
              v-model="form.completion"
              value=""
              :placeholder="$t('完成状态')"
            >
              <el-option
                v-for="dict in completionOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('日程描述')" prop="schedule">
            <el-input
              v-model="form.schedule"
              type="textarea"
              :placeholder="$t('日程描述')"
              show-word-limit
              maxlength="1000"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="week_schedule" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <!-- <save-btn has-permi="oa:weekSchedule" /> -->
      <el-button @click="cancel">{{ $t('取消') }}</el-button>
      <el-button v-if="form.showEdit || form.id === '' || form.id === undefined" v-hasPermi="['oa:weekSchedule:edit']" type="primary" @click="submitForm">{{ $t('保存') }}</el-button>
      <el-button v-show="form.showEdit && form.id !== '' && form.id !== undefined" type="info" @click="delData">{{ $t('删除') }}</el-button>
    </div>
  </div>
</template>

<script>
    import { saveOaWeekSchedule, getOaWeekSchedule, delOaWeekSchedule } from '@/api/oa/weekSchedule/oaWeekSchedule'
    import { getUser } from '@/api/system/user'
    export default {
      data() {
        const moneyValidCustom = (rule, value, callback) => {
          if (value === null || value === undefined || value === '') {
            return callback(new Error(this.$t('必填信息')))
          }
          if ((typeof this.form.startTime) === 'string' || (typeof this.form.finishTime) === 'string') {
            this.form.startTime = new Date(this.form.startTime)
            this.form.finishTime = new Date(this.form.finishTime)
          }
          if (this.form.startTime.getTime() > this.form.finishTime.getTime()) {
            return callback(new Error(this.$t('开始时间不能大于结束时间')))
          }
          callback()
          this.form.startTime = this.parseTime(this.form.startTime, '{y}-{m}-{d} {h}:{i}:{s}')
          this.form.finishTime = this.parseTime(this.form.finishTime, '{y}-{m}-{d} {h}:{i}:{s}')
        }
        return {
          otherBtnHide: false,
          // 表单参数
          form: {
            fileList: []
          },
          hasPermiDel: 'oa:weekSchedule:delete',
          travelOptions: [],
          completionOptions: [],
          // 遮罩层
          loading: false,
          // 表单校验
          rules: {
            // scheduleId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
            startTime: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
            schedule: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
            finishTime: [{ required: true, validator: moneyValidCustom, trigger: 'blur' }]
          }
        }
      },
        watch: {
          'form.scheduleStaffId': {
            handler(val) {
              if (val !== undefined && val !== null && val !== '') { this.userInfo(val) }
            },
            deep: true,
            immediate: true
          }
        },
        mounted() {
          this.$bus.$emit('async_done')
        },
        created() {
          this.getDicts('sys_yes_no').then(response => {
            this.travelOptions = response.data
          })
          this.getDicts('completion_status').then(response => {
            this.completionOptions = response.data
          })
        },
        methods: {
          // 初始化数据
          init(data) {
            this.loading = true
            if (data != null && data.id !== undefined) {
              getOaWeekSchedule(data.id).then(response => {
                this.form = response.data
                this.$nextTick(() => {
                  this.$refs.cityZj.initCity()
                })
                this.loading = false
              })
            } else {
              this.reset()
              if (data != null) {
                this.form.startTime = data.startTime
                this.form.finishTime = data.finishTime
              }
              this.form.travel = 'N'
              this.loading = false
            }
          },
            // 根据选择的用户不同显示部门
            userInfo(userId) {
              getUser(userId).then(response => {
                this.form.department = response.data.deptId
              })
            },
            // 表单重置
          reset() {
            this.form = {
              id: undefined,
              scheduleId: undefined,
              startTime: undefined,
              department: undefined,
              finishTime: undefined,
              travel: undefined,
              entourage: undefined,
              provinceRegistered: undefined,
              cityRegistered: undefined,
              completion: undefined,
              schedule: undefined,
              remind: 'w',
              remark: undefined,
              createBy: undefined,
              updateBy: undefined,
              createTime: undefined,
              updateTime: undefined,
              delFlag: undefined,
              dailyType: 'gr',
              publicScope: 'gk',
              fileList: []
            }
            this.form.scheduleStaffId = this.$store.state.user.userId
            this.resetForm('form')
          },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveOaWeekSchedule(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          delData() {
            this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
              confirmButtonText: this.$t('确定'),
              cancelButtonText: this.$t('取消'),
              type: 'warning'
            }).then(() => {
              return delOaWeekSchedule(this.form.id)
            }).then(() => {
              this.msgSuccess(this.$t('删除成功'))
              this.cancel()
            }).catch(function() {
            })
          }

        }

    }
</script>
